<template>
  <div v-outside="HandleHide" class="dropdown ilne-block">
    <!-- use： <template v-slot:[name]="slotProps"> -->
    <div @click="changeVisible">
      <slot :visible="oVisible">
        <a
          class="dropdown-toggle px-3 md:leading-11 leading-14.5 text-white"
          v-waves="['button', 'float']"
          href="javascript:void(0);"
          :style="oVisible ? { backgroundColor: 'rgba(0, 0, 0, 0.15)' } : undefined"
        >
          <span class="ml-1 hidden md:inline-block text-sm"
            >Dropdown <i class="mdi mdi-chevron-down"></i>
          </span>
        </a>
      </slot>
    </div>
    <!-- ******************************************* -->
    <ts-drop :margin-top="marginTop">
      <ul
        v-show="oVisible"
        class="dropdown-menu dropdown-menu-right dropdown-menu-animated"
        :class="menuClass"
      >
        <!-- use： <template v-slot:default> 或者 <template #default>  -->
        <slot name="dropdown"></slot>
      </ul>
    </ts-drop>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, inject } from "vue";
import { MittSymbol } from "@/utils/constant";
import { Emitter } from "mitt";
import clickOutside from "@/directives/clickoutside";
import TsDrop from "@/components/transition/TsDrop.vue";

export default defineComponent({
  name: "fo-dropdown",
  componentName: "FoDropdown",
  components: {
    [TsDrop.name]: TsDrop,
  },
  directives: {
    outside: clickOutside,
  },
  emits: ["command"],
  props: {
    marginTop: {
      type: [Number, String],
      default: 0,
    },
    menuClass: {
      type: [Array, String, Object],
      default: "",
    },
  },
  setup(props, context) {
    const emitter: Emitter | undefined = inject(MittSymbol);
    emitter &&
      emitter.on("menu-item-click", (command: any) => {
        // @ts-ignore
        context.emit("command", command);
      });

    const oVisible = ref(false);
    const changeVisible = () => {
      oVisible.value = !oVisible.value;
    };
    const HandleHide = () => {
      if (oVisible.value) {
        oVisible.value = false;
      }
    };
    return {
      oVisible,
      changeVisible,
      HandleHide,
    };
  },
});
</script>

<style lang="less">
.dropdown {
  position: relative;
  // 点击实体
  .dropdown-toggle {
    white-space: nowrap;
  }
  // 显示下拉menu
  .dropdown-menu {
    position: absolute;
    top: 100%;
    left: 0;
    z-index: 1000;
    float: left;
    min-width: 10rem;
    padding-top: 0.25rem;
    padding-bottom: 0.25rem;
    font-size: 1rem;
    color: #212529;
    text-align: left;
    background-color: #fff;
    background-clip: padding-box;
    border: 1px solid rgba(0, 0, 0, 0.15);
    border-radius: 0.25rem;
    box-shadow: 0 0 1.25rem rgba(108, 118, 134, 0.1);
    border-color: #eff3f6;
    &.dropdown-menu-right {
      // 默认在左边
      right: 0;
      left: auto;
      top: 100%;
      transform: none;
    }
    .dropdown-divider {
      height: 0;
      margin: 0.25rem 0;
      overflow: hidden;
      border-top: 1px solid #e9ecef;
    }
    .dropdown-item {
      display: block;
      width: 100%;
      padding: 0.25rem 1.5rem;
      clear: both;
      font-weight: 400;
      color: #212529;
      text-align: inherit;
      white-space: nowrap;
      background-color: transparent;
      border: 0;
      padding: 6px 16px;
      i {
        font-size: 16px;
        vertical-align: middle;
        margin-right: 7px;
      }
      .badge {
        display: inline-block;
        padding: 0.25em 0.4em;
        font-size: 75%;
        font-weight: 700;
        line-height: 1;
        text-align: center;
        white-space: nowrap;
        vertical-align: baseline;
        border-radius: 0.25rem;
        transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out,
          border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
        &.badge-success {
          color: #fff;
          background-color: #46cd93;
        }
      }
    }
  }
}

// .dropdown-menu's before
@media (min-width: 768px) {
  .dropdown-menu.tip:before {
    // .header-bg .navigation-menu > .has-submenu > .submenu:before
    content: "";
    position: absolute;
    width: 12px;
    height: 12px;
    background: #ffffff;
    top: -6px;
    left: 18px;
    -webkit-transform: rotate(45deg);
    transform: rotate(45deg);
  }
  .dropdown-menu-right.tip:before {
    right: 18px;
    left: auto;
  }
}
</style>
